/**
 * BBD Service Inc
 * All Rights Reserved @2018
 */
package com.magicmovie.web.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.magicmovie.dto.MovieDTO;
import com.magicmovie.entity.Constants;
import com.magicmovie.entity.Dictionary;
import com.magicmovie.entity.Movie;
import com.magicmovie.utils.AjaxMsg;
import com.magicmovie.utils.JqgridPage;
import com.magicmovie.utils.QueryFilters;
import com.magicmovie.utils.ValidUtil;
import com.magicmovie.utils.data.PageModel;
import com.magicmovie.web.service.DictionaryService;
import com.magicmovie.web.service.MovieService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 輪播管理
 *
 * @author
 */
@Controller
public class MovieController {
    @Resource
    private MovieService movieService;
    @Resource
    private DictionaryService dictionaryService;

    @RequestMapping({"/movie/list"})
    public String list(Model model) {
        Map map = new HashMap<>();
//        List list = movieService.queryList(map);
        List<Dictionary> dictionaries = dictionaryService.queryByParams(map);
//        model.addAttribute("list", list);
        model.addAttribute("dictionaries", dictionaries);
        return "product/list";
    }

    @RequestMapping("/movie/query")
    @ResponseBody
    public JqgridPage query(int page, int rows, String sidx, String sord, String filters) {
        QueryFilters filters_ = null;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        filters_.setPageNo(page);
        filters_.setPageSize(rows);
        PageModel pageInfo = movieService.getByFilter(filters_);
        // 用JqgridPage对结果进行包装
        return new JqgridPage((long) pageInfo.getTotalRecord(), pageInfo.getTotalPage(), pageInfo.getPageNo(), pageInfo.getResults());
    }

    @RequestMapping(value = "/movie/input")
    public String input(Model model, HttpServletRequest request, Integer id) {
        // 是否为编辑保存
        Movie movie;
        String isEdit;
        if (id != null && id != 0) {
            movie = movieService.selectByPramaryKey(id);
            isEdit = "edit";
            model.addAttribute("movie", movie);
        } else {
            isEdit = "add";
            String object = request.getParameter("object");
            model.addAttribute("object", object);
        }

        model.addAttribute("isEdit", isEdit);
        Map map = new HashMap();
        List<Dictionary> dictionaries = dictionaryService.queryByParams(map);
        model.addAttribute("dictionaries", dictionaries);
        return "product/input";
    }

    @RequestMapping(value = "/movie/save")
    public String save(MovieDTO movie) {
        String images = movie.getImage();
        if (!ValidUtil.isEmpty(images)) {
            images = images.substring(0, images.length() - 1);
            movie.setImage(images);
        }
        movieService.save(movie);
        return "redirect:/product/list";
    }

    @RequestMapping("/movie/oper")
    @ResponseBody
    public AjaxMsg oper(HttpServletResponse response, String oper, Movie movie) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setCode(AjaxMsg.SUCCESS);
        if (oper.equals("del")) {
            //movieService.delete(movie.getId());
            movie = movieService.selectByPramaryKey(movie.getId());
            if (Constants.MOVIE_STATUS_UP.equals(movie.getStatus())) {
                movie.setStatus(Constants.MOVIE_STATUS_DOWN);
            } else {
                movie.setStatus(Constants.MOVIE_STATUS_UP);
            }
            movieService.update(movie);
        } else if (oper.equals("edit")) {
        } else if (oper.equals("add")) {
        } else {
            ajaxMsg.setCode("500");
            ajaxMsg.setMessage("操作代码错误！系统只提供增加/修改/删除功能。");
            return ajaxMsg;
        }
        return ajaxMsg;
    }


}
